# -*- coding: utf-8 -*-
'''
Author:     郭国辉
CreateTime:     2018年2月24日    上午10:10:51
'''

import pygame

from com.ggh.plane_fight.main.game_sprite import BackgroundSprite, EnemySprite, HeroSprite
from com.ggh.plane_fight.main.game_sprite import SCREEN_RECT
from com.ggh.plane_fight.main.game_sprite import FRAMES_PER_SECOND
from com.ggh.plane_fight.main.game_sprite import ENEMY_EVENT
from com.ggh.plane_fight.main.game_sprite import HERO_SPEED
from com.ggh.plane_fight.main.game_sprite import HERO_FIRE_EVENT
import file_utils

class PlaneGame:
    '''飞机大战主游戏类'''
    def __init__(self):
        print("游戏初始化")
        pygame.init()
        #摧毁敌机数量
        self.hero_score=0
        #创建游戏窗口
        self.screen=pygame.display.set_mode(SCREEN_RECT.size)
        #创建游戏时钟
        self.clock=pygame.time.Clock();
        #调用私有方法，精灵和精灵组的创建
        self.__create_sprite();
        
        #创建敌机定时器事件，创建敌机
        pygame.time.set_timer(ENEMY_EVENT,100)
        #创建英雄发射子弹定时器事件，创建子弹
        pygame.time.set_timer(HERO_FIRE_EVENT,100)
        
    def __create_sprite(self):
        bg1=BackgroundSprite("%s\\images\\bg_01.png"%file_utils.get_root());
        bg2=BackgroundSprite("%s\\images\\bg_01.png"%file_utils.get_root());
        bg2.rect.y=-bg2.rect.height;
        self.bg_group=pygame.sprite.Group(bg1,bg2);
        
        #创建敌机精灵组
        self.enemy_group=pygame.sprite.Group();
        
        #创建英雄精灵和精灵组
        self.hero_sprite=HeroSprite("%s\\images\\hero.png"%file_utils.get_root())
        self.hero_group=pygame.sprite.Group(self.hero_sprite);        
             
    def start_game(self):
        print("游戏开始");
        while True:
            #设置刷新帧率
            self.clock.tick(FRAMES_PER_SECOND)
            #事件监听
            self.__event_handler()
            #碰撞检测
            self.__check_collide()
            #更新绘制精灵组
            self.__update_sprite()
            #更新显示
            pygame.display.update()
            
    def __event_handler(self):
        eventList=pygame.event.get();
        for event in eventList:
            if event.type==pygame.QUIT:
                PlaneGame.__game_over()
            elif event.type==ENEMY_EVENT:
                enemy_sprite=EnemySprite("%s\\images\\enemy_1.png"%file_utils.get_root());
                self.enemy_group.add(enemy_sprite);
            elif event.type==HERO_FIRE_EVENT:
                self.hero_sprite.fire()
        #使用键盘提供的方法获取键盘元组
        keys_pressed=pygame.key.get_pressed();
        
        if keys_pressed[pygame.K_RIGHT]:
            self.hero_sprite.speed=HERO_SPEED
        elif keys_pressed[pygame.K_LEFT]:
            self.hero_sprite.speed=-HERO_SPEED
        else:
            self.hero_sprite.speed=0
            
    def __check_collide(self):
        #子弹-敌机碰撞检测
        killers=pygame.sprite.groupcollide(self.hero_sprite.bullet_group, self.enemy_group, True, True)
        if len(killers)>0:
            self.hero_score+=1
        #英雄-敌机碰撞检测
        enemies=pygame.sprite.spritecollide(self.hero_sprite, self.enemy_group, True)
        
        if len(enemies)>0:
            print(self.hero_score)
            PlaneGame.__game_over()
    def __update_sprite(self):
        self.bg_group.update()
        self.bg_group.draw(self.screen)
        
        #敌机精灵组更新
        self.enemy_group.update()
        self.enemy_group.draw(self.screen)
    
        #英雄精灵组更新
        self.hero_group.update()
        self.hero_group.draw(self.screen)
        
        #子弹精灵组更新
        self.hero_sprite.bullet_group.update()
        self.hero_sprite.bullet_group.draw(self.screen)
        
    @staticmethod
    def __game_over():
        print("游戏结束。。。")
        pygame.quit()
        exit()

if __name__ == '__main__':
    pg=PlaneGame()
    pg.start_game()